/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Inject, Injectable } from '@angular/core';
import { Service } from '@api';
import { DA_SERVICE_TOKEN, ITokenModel, ITokenService } from '@delon/auth';
import { _HttpClient } from '@delon/theme';
import { environment } from '@env/environment';
import { OssFile } from '../model/ossFile.model';

/**
 * 文件信息业务接口
 * @author maoxiaodong
 */
@Injectable()
export class OssFileService extends Service<OssFile, string> {
  constructor(public http: _HttpClient, @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService) {
    super(http, `${environment.oss.ctx}}/ossFile`);
  }

  download(path: string) {
    let url = `${environment.SERVER_URL}${path}`;
    const tokenData: ITokenModel = this.tokenService.get() as ITokenModel;
    if (tokenData.token) {
      if (url.indexOf('?') == -1) {
        url += `?access_token=${tokenData.token}`;
      } else {
        url += `&access_token=${tokenData.token}`;
      }
    }
    const a = document.createElement('a');
    a.href = url;
    a.target = '_blank';
    a.click();
  }
}
